<?php

namespace frontend\controllers;
/**
 * 会员控制器
 */

use frontend\models\UserForm;
use yii;
use common\models\UserModel;
use frontend\models\PostsForm;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\web\NotFoundHttpException;

class MemberController extends base\BaseController
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        //是否要求登录的控制器方法控制
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['index', 'Edit','upload','ueditor','mylist'],
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['Edit','upload','ueditor','mylist'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    '*' => ['get', 'post'],
                ],
            ],
        ];
    }
    public function actions()
    {
        return [
            'upload'=>[
                'class' => 'common\widgets\file_upload\UploadAction',     //这里扩展地址别写错
                'config' => [
                    'imagePathFormat' => "/image/{yyyy}{mm}{dd}/{time}{rand:6}",
                ]
            ],
            'ueditor'=>[
                'class' => 'common\widgets\ueditor\UeditorAction',
                'config'=>[
                    //上传图片配置
                    'imageUrlPrefix' => "", /* 图片访问路径前缀 */
                    'imagePathFormat' => "/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
                ]
            ]
        ];
    }
    /**
     * 其他博主博客主页
     */
    public function actionIndex()
    {
        $data = [];
        $data['search_date'] = yii::$app->request->get('date','');
        $data['user_id'] = yii::$app->request->get('id','');
        if(!$data['user_id']){
            throw new NotFoundHttpException('参数错误');
        }
        $model = new PostsForm();
        $data['date_arr'] = $model->getMylistDateArr($data['user_id']);
        $data['user_info'] = UserModel::findOne($data['user_id']);
        $data['user_count'] = (new PostsForm())->getMyPostCount($data['user_id']);
        return $this->render('index',['data'=>$data]);
    }

    /**
     * 我的资料
     */
    public function actionEdit()
    {
        $data = [];
        $user_id = yii::$app->user->identity->getId();
        $model = new UserForm();
        $model->setScenario(UserForm::SCENARIO_UPDATE);
        if($model->load(Yii::$app->request->post()) && $model->validate()){
            if(!$model->edit()){
                Yii::$app->session->setFlash('warning', $model->_lastError);
            }else{
                return $this->redirect(['/member/edit']);
            }
        }

        $item = $model->getItem($user_id);
        $model->id = $user_id;
        $model->username = $item['username'];
        $model->email = $item['email'];
        $model->head_img = $item['head_img'];
//var_dump($model);die;
        return $this->render('edit',['model'=> $model]);

    }

    /**
     * 我的资料
     */
    public function actionSetpwd()
    {
        $model = new UserForm();
        if(Yii::$app->request->post()){
            $password = yii::$app->request->get('password_hash','');
//            var_dump(Yii::$app->request->post()['UserForm']['password_hash']);
//            echo $password;die;
            if(!$model->SetPwd(Yii::$app->request->post()['UserForm']['password_hash'])){
                Yii::$app->session->setFlash('warning', $model->_lastError);
            }else{
                return $this->redirect(['/site/index']);
            }
        }
        return $this->render('setpwd',['model'=> $model]);
    }

}
